function showPasswordInput() {
	document.getElementById('password-div').innerHTML = '<label>Password:</label> <input id="password" name="password" type="password" class="user-form-input" type="text"><button id="showPasswordButton" class="btn btn-small" type="button" onclick="showPassword()">Show</button>';
}

function showPassword() {
	var pass = $('#password')[0].value;
	document.getElementById('password-div').innerHTML = '<label>Password:</label> <input id="password" name="password" type="text" value ="'
			+ pass
			+ '" class="user-form-input" type="text"><button id="showPasswordButton" class="btn btn-small" type="button" onclick="hidePassword()">Hide</button>';
}

function hidePassword() {
	var pass = $('#password')[0].value;
	document.getElementById('password-div').innerHTML = '<label>Password:</label> <input id="password" name="password" type="password" value ="'
			+ pass
			+ '" class="user-form-input" type="text"><button id="showPasswordButton" class="btn btn-small" type="button" onclick="showPassword()">Show</button>';
}
function showEditGroupMembershipDialog() {
	var collectionName = 'groups';

	var modalElem = $('#groups-preview-modal');
	var modalBody = $('#groups-preview-modal>#groups-preview-modal-body')
			.empty();

	var items = $("input[id*='" + collectionName + "'][type='hidden']");
	var idList = "";

	for ( var i = 0; i < items.length; i++) {
		idList = idList + (idList.length > 0 ? "," : "") + items[i].value;
	}

	$.ajax({
		type : "post",
		url : applicationName + '/admin/user/getGroups/',
		data : "list=" + idList
	}).done(function(result) {
		modalBody.html(result);
		showModal(modalElem);
	});

	modalBody.empty();
}

function updateGroupsList() {
	var collectionName = 'groups';

	var groupListElement = $("div#userGroupList");
	var itemsCount = groupListElement.children().size();
	var selectedElems = $("input[id*='modal_glist_'][type='checkbox']:checked");

	for ( var i = 0; i < selectedElems.length; i++) {
		var id = selectedElems[i].value;

		var div = document.createElement("div");
		var hiddenInput = document.createElement("input");
		var checkboxInput = document.createElement("input");
		var aLink = document.createElement("a");

		div.className = 'clearfix';

		hiddenInput.id = collectionName + itemsCount + ".id";
		hiddenInput.name = collectionName + "[" + itemsCount + "].id";
		hiddenInput.type = "hidden";
		hiddenInput.value = id;

		checkboxInput.id = collectionName + '_checkbox_' + id;
		checkboxInput.type = 'checkbox';

		aLink.href = applicationName + '/admin/group/' + id;
		aLink.innerHTML = selectedElems[i].parentNode.parentNode.children[1].textContent;

		div.appendChild(checkboxInput);
		div.appendChild(aLink);
		div.appendChild(hiddenInput);

		groupListElement.append(div);

		itemsCount++;
	}
}

function removeGroupDivs() {
	var selectedElements = $("div#userGroupList > div > input[id=*'groups_checkbox_'][type='checkbox']:checked");

	selectedElements.parent().remove();
	updateGroupIndexes();
}

function updateGroupIndexes() {
	var collectionName = 'groups';

	var items = $("input[id*='" + collectionName + "'][type='hidden']");

	for ( var i = 0; i < items.length; i++) {
		items[i].id = collectionName + i + ".id";
		items[i].name = collectionName + "[" + i + "].id";
	}
}